package com.example.word.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.dict.R;
import com.example.word.adapter.CollectAdapter;
import com.example.word.bean.Collect;
import com.example.word.bean.Dict;
import com.example.word.bean.Word;
import com.example.word.util.HttpUtils;
import com.example.word.util.KeyBoardUtil;
import com.example.word.util.SPUtils;
import com.example.word.util.StringUtils;
import com.example.word.widget.ActionBar;

import org.litepal.crud.DataSupport;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收藏记录
 */
public class CollectActivity extends AppCompatActivity {
    private Activity myActivity;
    private ActionBar mTitleBar;//标题栏
    private LinearLayout llEmpty;
    private RecyclerView rvCollectList;
    private CollectAdapter mCollectAdapter;
    private List<Collect> mCollect;
    private String account,searchText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        myActivity = this;
        rvCollectList = findViewById(R.id.rv_collect_list);
        llEmpty = findViewById(R.id.ll_empty);
        mTitleBar = (ActionBar) findViewById(R.id.myActionBar);
        mTitleBar.setData(myActivity, "收藏记录", R.drawable.ic_back, R.drawable.search, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
                searchClick();
            }
        });
        // 设置主题
        if (!com.example.word.ui.activity.MainActivity.colorChange) {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        } else {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.myColor));
        }
        initView();
    }

    private void initView() {
        account = (String) SPUtils.get(myActivity, SPUtils.ACCOUNT, "");
        LinearLayoutManager layoutManager = new LinearLayoutManager(myActivity);
        //=1.2、设置为垂直排列，用setOrientation方法设置(默认为垂直布局)
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //=1.3、设置recyclerView的布局管理器
        rvCollectList.setLayoutManager(layoutManager);
        //==2、实例化适配器
        //=2.1、初始化适配器
        mCollectAdapter = new CollectAdapter(llEmpty, rvCollectList);
        //=2.3、设置recyclerView的适配器
        rvCollectList.setAdapter(mCollectAdapter);
        loadData();//加载数据
        mCollectAdapter.setItemListener(new CollectAdapter.ItemListener() {
            @Override
            public void ItemClick(Collect collect) {
                Map<String, String> requestParam = new HashMap<>();
                requestParam.put("word", collect.getWord());
                // 调用 API 客户端的方法
                HttpUtils.sendGetRequest("/getWordList", requestParam, myActivity, Word.class, response -> {
                    List<Word> wordList  = response.getDataList();
                    Intent intent = new Intent(myActivity, WordStudyActivity.class);
                    intent.putExtra("wordArray", new String[]{wordList.get(0).getWord()});
                    intent.putExtra("dict", wordList.get(0).getDict());
                    startActivityForResult(intent, 100);
                });
            }
        });
    }

    /**
     * 加载数据
     */
    private void loadData(String... content) {
        Map<String, String> requestParam = new HashMap<>();
        if (content.length > 0) {
            requestParam.put("wordLike", content[0]);
        }
        HttpUtils.sendGetRequest("/getCollectList", requestParam, myActivity, Collect.class, collectResponse -> {
            mCollect = collectResponse.getDataList();
            if (mCollect != null && mCollect.size() > 0) {
                rvCollectList.setVisibility(View.VISIBLE);
                llEmpty.setVisibility(View.GONE);
                mCollectAdapter.addItem(mCollect);
            } else {
                rvCollectList.setVisibility(View.GONE);
                llEmpty.setVisibility(View.VISIBLE);
            }
        });

    }

    public void searchClick() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.a1);//设置图标
        builder.setTitle("搜索");
        // 创建一个 EditText 控件用于输入
        final EditText inputEditText = new EditText(this);
        inputEditText.setHint("请输入搜索关键字");
        if (StringUtils.isNotEmpty(searchText)) {
            inputEditText.setText(searchText);
        }
        builder.setView(inputEditText);
        // 设置对话框的积极按钮（确定按钮）以及点击事件
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ProgressDialog searchDialog = new ProgressDialog(myActivity);
                searchDialog.setMessage("正在搜索···");
                searchDialog.show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 在这里处理确定按钮的点击事件，可以获取输入框中的文本
                        String userInput = inputEditText.getText().toString();
                        searchText = userInput;
                        // 处理用户输入...
                        loadData(userInput);
                        searchDialog.dismiss();
                        dialog.dismiss();
                    }
                }, 505);
            }
        });
        builder.setNegativeButton("重置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        searchText = "";
                        loadData();
                    }
                }
        );
        AlertDialog a = builder.create();
        a.show();
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }
}